//
//  BaseADViewController.m
//  TestDialogue
//
//  Created by Hepburn Alex on 13-2-6.
//  Copyright (c) 2013年 Hepburn Alex. All rights reserved.
//

#import "BaseADViewController.h"

#define MB_RELEASE(exp)

@interface BaseADViewController ()

@end

@implementation BaseADViewController

@synthesize mlbTitle, delegate, OnGoBack, mShadowImage, mTopColor, mTopImage, mLoadMsg, mTitleColor, mNavType, mFontSize,mLoadView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        mbStatusBarHidden = NO;
        mStatusBarStyle = UIStatusBarStyleDefault;
        mNavType = TNavBarType_Normal;
        mFontSize = 20;
        self.mTopImage = [UIImage imageNamed:@"topbar7.png"];
        self.mTopColor = ComColor;
        self.mTitleColor = [UIColor whiteColor];
        
        self.mShadowImage = [UIImage imageNamed:@"shdow-t.png"];
    }
    return self;
}

//- (id)initWithCoder:(NSCoder *)aDecoder {
//    self = [super initWithCoder:aDecoder];
//    if (self) {
//        mbStatusBarHidden = NO;
//        mStatusBarStyle = UIStatusBarStyleDefault;
//        mNavType = TNavBarType_Normal;
//        mFontSize = 16;
//        self.mTopImage = [UIImage imageNamed:@"topbar7.png"];
//        self.mTopColor = [UIColor whiteColor];
//        self.mTitleColor = [UIColor blackColor];
//        self.mShadowImage = [UIImage imageNamed:@"shdow-t.png"];
//    }
//    return self;
//}

- (void)GoHome {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)GoBack {
    if (delegate && OnGoBack) {
        SafePerformSelector([delegate performSelector:OnGoBack withObject:self]);
    }
    UIViewController *topCtrl = self.navigationController.topViewController;
    if (topCtrl == self) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)RefreshNavColor:(UIColor *)color {
    self.mTopColor = color;
    [self RefreshNavColor];
}

- (void)RefreshNavColor {
    if (mTopColor) {
        self.navigationController.navigationBar.backgroundColor = mTopColor;
        self.navigationController.navigationBar.barTintColor = mTopColor;
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    }
    if (mTopImage) {
        [self.navigationController.navigationBar setBackgroundImage:mTopImage forBarMetrics:UIBarMetricsDefault];
    }
    if (_mBlurView) {
        [self.view bringSubviewToFront:_mBlurView];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mfScale = self.view.frame.size.width/320;
    self.view.backgroundColor = DEFAULT_BACKCOLOR;
    //去掉边缘化 适配
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = (mNavType != TNavBarType_Normal);
    if (mNavType == TNavBarType_Alpha) {
        self.edgesForExtendedLayout = UIRectEdgeAll;
        self.automaticallyAdjustsScrollViewInsets = YES;
        self.navigationController.navigationBar.translucent = YES;
        self.mTopImage = [UIImage imageNamed:@"f_topblurbar"];
        if (_mBlurView) {
            [self.view addSubview:_mBlurView];
        }
    }
    [self RefreshNavColor];
    if (mShadowImage) {
        mShadowView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 4)];
        mShadowView.alpha = 0.8;
        mShadowView.image = mShadowImage;
        mShadowView.tag = 1919;
        [self.view addSubview:mShadowView];
    }

    mlbTitle = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, self.view.frame.size.width-100, 44)];
    mlbTitle.backgroundColor = [UIColor clearColor];
    mlbTitle.font = [UIFont fontWithName:@"Helvetica-Bold" size:mFontSize];
//    mlbTitle.font = [UIFont systemFontOfSize:mFontSize];
    mlbTitle.textAlignment = NSTextAlignmentCenter;
    mlbTitle.textColor = self.mTitleColor;
    mlbTitle.text = self.title;
    self.navigationItem.titleView = mlbTitle;
    
    [self AddLeftImageBtn:nil target:nil action:nil];
    [self AddRightTextBtn:nil target:nil action:nil];
    
    mlbMsg = [[UILabel alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height-40)/2, self.view.frame.size.width, 40)];
    mlbMsg.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    mlbMsg.backgroundColor = [UIColor clearColor];
    mlbMsg.font = [UIFont systemFontOfSize:16];
    mlbMsg.textColor = [UIColor darkGrayColor];
    mlbMsg.text = @"暂无数据";
    mlbMsg.textAlignment = NSTextAlignmentCenter;
    mlbMsg.hidden = YES;
    [self.view addSubview:mlbMsg];
}

- (void)ClearNavItem {
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem = nil;
}

- (UIButton *)GetImageButton:(UIImage *)image target:(id)target action:(SEL)action scale:(float)scale left:(BOOL)bLeft {
    int iBtnWidth = 40;
    int iWidth = image.size.width/scale;
    int iHeight = image.size.height/scale;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, iBtnWidth, 40);
    [rightBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(bLeft?0:(iBtnWidth-iWidth), (40-iHeight)/2, iWidth, iHeight);
    imageView.tag = 1300;
    [rightBtn addSubview:imageView];
    
    return rightBtn;
}

- (UIBarButtonItem *)GetImageBarItem:(UIImage *)image target:(id)target action:(SEL)action scale:(float)scale left:(BOOL)bLeft {
    UIButton *rightBtn = [self GetImageButton:image target:target action:action scale:scale left:bLeft];
    return [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

- (UIButton *)GetTextButton:(NSString *)text target:(id)target action:(SEL)action {
//    UIFont *font = [UIFont systemFontOfSize:13];
    UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:14];

    NSDictionary *attribute = @{ NSFontAttributeName:font };
    CGSize calcSize = [text sizeWithAttributes:attribute];
    
    int iWidth = calcSize.width<30?30:calcSize.width+4;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, iWidth, 40);
    [rightBtn setTitle:text forState:UIControlStateNormal];
    [rightBtn setTitleColor:self.mTitleColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = font;
    [rightBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return rightBtn;
}

- (UIBarButtonItem *)GetTextBarItem:(NSString *)name target:(id)target action:(SEL)action {
    UIButton *btn = [self GetTextButton:name target:target action:action];
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

- (void)AddLeftImageBtn:(UIImage *)image target:(id)target action:(SEL)action {
    [self AddLeftImageBtn:image target:target action:action scale:2.0];
}

- (void)AddLeftImageBtn:(UIImage *)image target:(id)target action:(SEL)action scale:(float)scale {
    self.navigationItem.leftBarButtonItem = [self GetImageBarItem:image target:target action:action scale:scale left:YES];
}

- (void)AddRightImageBtn:(UIImage *)image target:(id)target action:(SEL)action {
    [self AddRightImageBtn:image target:target action:action scale:2.0];
}

- (void)AddRightImageBtn:(UIImage *)image target:(id)target action:(SEL)action scale:(float)scale  {
    self.navigationItem.rightBarButtonItem = [self GetImageBarItem:image target:target action:action scale:scale left:NO];
}

- (void)AddLeftTextBtn:(NSString *)name target:(id)target action:(SEL)action {
    self.navigationItem.leftBarButtonItem = [self GetTextBarItem:name target:target action:action];
}

- (void)AddRightTextBtn:(NSString *)name target:(id)target action:(SEL)action {
    self.navigationItem.rightBarButtonItem = [self GetTextBarItem:name target:target action:action];
}

- (void)AddLeftImageBtns:(NSArray *)array {
    if (!array || array.count == 0) {
        return;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, array.count*40, 40)];
    for (int i = 0; i < array.count; i ++) {
        UIButton *btn = [array objectAtIndex:i];
        btn.frame = CGRectMake(i*40, 0, 40, 40);
        [view addSubview:btn];
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
}

- (void)AddRightImageBtns:(NSArray *)array {
    if (!array || array.count == 0) {
        return;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, array.count*40, 40)];
    for (int i = 0; i < array.count; i ++) {
        UIButton *btn = [array objectAtIndex:i];
        btn.frame = CGRectMake(i*40, 0, 40, 40);
        [view addSubview:btn];
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
}
- (void)AddRightImageBtns:(NSArray *)array target:(id)target action:(SEL)action
{
    if (!array || array.count == 0) {
        return;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, array.count*44, 44)] ;
    view.backgroundColor = [UIColor clearColor];
    
    for (int i = 0; i < array.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(6+i*44, 0, 44, 44);
        btn.tag = i;
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        
        UIImage *image = [UIImage imageNamed:[array objectAtIndex:i]];
        int iWidth = image.size.width;
        int iHeight = image.size.height;
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake((44-iWidth)-6, (44-iHeight)/2, iWidth, iHeight);
        imageView.backgroundColor = [UIColor clearColor];
        imageView.image = image;
        imageView.tag = 1301;
        [btn addSubview:imageView];
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
}
- (void)AddLeftImageBtns:(NSArray *)array target:(id)target action:(SEL)action
{
    if (!array || array.count == 0) {
        return;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, array.count*44, 44)];
    view.backgroundColor = [UIColor clearColor];
    
    for (int i = 0; i < array.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*44, 0, 44, 44);
        btn.tag = i;
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        
        UIImage *image = [UIImage imageNamed:[array objectAtIndex:i]];
        int iWidth = image.size.width/2;
        int iHeight = image.size.height/2;
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(0, (44-iHeight)/2, iWidth, iHeight);
        imageView.backgroundColor = [UIColor clearColor];
        imageView.image = image;
        imageView.tag = 1302;
        [btn addSubview:imageView];
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
}

- (void)setTitle:(NSString *)title {
    [super setTitle:title];
    mlbTitle.text = title;
}

- (UIView *)GetInputAccessoryView {
    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 40)];
    inputView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
      UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(inputView.frame.size.width - 50, 0, 50, inputView.frame.size.height);
    btn.backgroundColor = [UIColor clearColor];
    [btn setTitle:@"隐藏" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];

    [btn addTarget:self action:@selector(OnHideKeyboard) forControlEvents:UIControlEventTouchUpInside];
    [inputView addSubview:btn];
//
    return inputView;
}

- (void)OnHideKeyboard {
    [self.view endEditing:NO];
}

- (void)dealloc {
    self.mShadowImage = nil;
    self.mTopColor = nil;
    self.mTopImage = nil;
    self.mLoadMsg = nil;
}

- (void)StartLoading
{
    if (mLoadView) {
        return;
    }
    mLoadView = [[MBProgressHUD alloc] initWithView:self.view];
    
    if (mLoadMsg) {
        mLoadView.mode = MBProgressHUDModeCustomView;
        mLoadView.labelText = mLoadMsg;
    }
    [self.view addSubview:mLoadView];
    MB_RELEASE(mLoadView);
    
    [mLoadView show:YES];
}


- (void)StopLoading
{
    [mLoadView hide:YES];
    mLoadView = nil;
}

- (void)showMsg:(NSString *)msg
{
    mLoadView = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:mLoadView];
    MB_RELEASE(mLoadView);
    
    mLoadView.mode = MBProgressHUDModeCustomView;
    mLoadView.labelText = msg;
    [mLoadView show:YES];
    [mLoadView hide:YES afterDelay:1];
    mLoadView = nil;
}


- (void)RefreshShadow {
    UIView *shadowView = [self.view viewWithTag:1919];
    if (shadowView) {
        [self.view bringSubviewToFront:shadowView];
    }
}


- (void)HideStatusBar:(BOOL)hide {
    mbStatusBarHidden = hide;
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
}

- (void)RefreshStatusBarStyle:(UIStatusBarStyle)barStyle {
//    NSLog(@"RefreshStatusBarStyle");
    mStatusBarStyle = barStyle;
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        [self preferredStatusBarStyle];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
}
//设置导航栏的颜色
- (void)setTopColor:(UIColor *)color{
    self.navigationController.navigationBar.barTintColor = color;
}

//改变导航栏上文字的颜色
- (void)settopTextColor:(UIColor *)color{
    if (mlbTitle) {
        mlbTitle.textColor = color;
    }
}
- (BOOL)prefersStatusBarHidden
{
    return mbStatusBarHidden;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    NSLog(@"preferredStatusBarStyle :%@", [self class]);
    return mStatusBarStyle;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self RefreshShadow];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
